# coding: utf-8
import json


# ----------------------------------------------------------
def main_process_json(dict_data):
    key_w = dict_data.pop("ip")
    value_w = dict(dict_data)

    with open("data.json", "r", encoding="utf-8") as f:
        old_data = json.load(f)
        if key_w in old_data:
            value_w["access_times"] = old_data[key_w]['access_times'] + 1  # 如有重复访问，则此ip次数加一
        else:
            value_w["access_times"] = 1

        new_dict = {key_w: value_w}
        print("record data :", new_dict)
        old_data.update(new_dict)

        total_num = 0
        for key in old_data.keys():
            total_num += old_data[key]["access_times"]
        rt_list = {"total_people": + len(old_data) , "total_times": total_num }

    with open("data.json", "w", encoding="utf-8") as f:
        # json.dump(old_data, f, ensure_ascii=False)
        js_data = json.dumps(old_data, indent=4, separators=(',', ':')).encode('utf-8').decode('raw_unicode_escape')

        f.write(js_data)

    return rt_list


def return_data():
    with open("data.json", "r", encoding="utf-8") as f:
        json_data = json.load(f)
        total_num = 0
        for key in json_data.keys():
            total_num += json_data[key]["access_times"]
            # print(key + ':' + str(json_data[key]["access_times"]))
        print("总人数 ", len(json_data))
        print("总次数 ", total_num)


if __name__ == '__main__':
    # dict1 = {"Country": "中国", "Province": "安徽省", "city": "滁州市", "ip": "120.242.21.32", "isp": "移动","date": "2020/12/12"}
    # dict2 ={"ip":"107.182.27.229","Country":"美国","Province":"加利福尼亚","city":"0","isp":"0","date":"2021·0·22·0:04","access_times":1 }
    # main_process_json(dict1)
    return_data()
